package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.enums.ArticleStatus;
import com.example.forum.common.enums.BoardStatus;
import com.example.forum.common.enums.ResultCode;
import com.example.forum.common.validation.ValidationUtils;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.services.IAdminService;
import com.example.forum.services.IArticleService;
import com.example.forum.services.IBoardService;
import com.example.forum.services.IUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hanzishuai
 * @date 2025/4/30 11:35
 * @Description 禁用和删除 帖子、回复帖、板块、站内信、用户
 */
@Slf4j
@Service
public class AdminService implements IAdminService {

    @Resource
    private IArticleService articleService;


    @Resource
    private IBoardService boardService;

    @Resource
    private IUserService userService;


    @Override
    public void deleteArticle(Long articleId) {
        ValidationUtils.id(articleId, "帖子 Id");
        articleService.deleteById(articleId);
    }

    @Override
    public void banArticle(Long articleId) {
        ValidationUtils.id(articleId, "帖子 Id");


    }


    @Override
    public void deleteArticleReply(Long articleReplyId) {
        ValidationUtils.id(articleReplyId, "帖子回复 Id");

    }


    @Override
    public void banArticleReply(Long articleReplyId) {
        ValidationUtils.id(articleReplyId, "帖子回复 Id");

    }


    @Override
    public void banUser(Long userId) {

    }


    @Override
    @Transactional
    public void deleteBoard(Long boardId) {
        ValidationUtils.id(boardId, "板块 Id");
        Board board = boardService.selectById(boardId);
        ValidationUtils.banned(board);

        LocalDateTime now = LocalDateTime.now();
        // 构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(boardId);
        updateBoard.setDeleteState(BoardStatus.IS_DELETE.getStatus());
        updateBoard.setUpdateTime(now);

        boardService.update(updateBoard);

        // 根据板块 id 查询所有的帖子数据
        List<Article> articles = articleService.selectAllByBoardId(boardId);
        if (articles != null && !articles.isEmpty()) {
            for (Article article : articles) {
                Article updateArticle = new Article();
                updateArticle.setId(article.getId());
                updateArticle.setDeleteState(ArticleStatus.IS_DELETE.getStatus());
                updateArticle.setUpdateTime(now);
                // 删除当前板块对应的帖子
                articleService.updateByPrimaryKeySelective(updateArticle);
                // 减少对应的用户发帖数
                userService.subOneArticleCountById(article.getUserId());
            }
        }


    }

    @Override
    public void banBoard(Long boardId) {
        ValidationUtils.id(boardId, "板块 Id");
        Board board = boardService.selectById(boardId);
        ValidationUtils.banned(board);

        // 构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(boardId);
        updateBoard.setState(BoardStatus.IS_BANNED.getStatus());
        updateBoard.setUpdateTime(LocalDateTime.now());
        boardService.update(updateBoard);
    }

    @Override
    public void addBoard(Board board) {
        if (board == null || board.getName() == null || board.getSort() == null
                || board.getSort() < 0
                || !StringUtils.hasLength(board.getName())) {
            log.warn("{}", ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> boards = boardService.selectByName(board.getName());
        if (!boards.isEmpty()) {
            log.warn("{}", ResultCode.FAILED_BOARD_EXISTS);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_EXISTS));
        }

        // 构造板块对象
        board.setArticleCount(0);
        board.setState(BoardStatus.NOT_BANNED.getStatus());
        board.setDeleteState(BoardStatus.NOT_DELETE.getStatus());
        LocalDateTime date = LocalDateTime.now();
        board.setCreateTime(date);
        board.setUpdateTime(date);

        // 写入数据库
        boardService.create(board);

        log.info("新增板块成功，板块名称：{}", board.getName());
    }


}




















